package com.huazai.apply.manager.web.action;


import com.huazai.apply.manager.bean.User;
import com.huazai.apply.manager.service.UserInitService;
import com.huazai.apply.manager.service.impl.UserInitServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * @author EZTTU
 */
@WebServlet(name = "Init", value = {"/login", "/register"})
public class UserInitServlet extends HttpServlet {
    private static final String SERVICE_LOGIN = "/login";
    private static final String SERVICE_REGISTER = "/register";

    private static final String MES_LOGIN_SUCCESS = "登录成功";

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String servletPath = request.getServletPath();

        if (servletPath.equals(SERVICE_LOGIN)) {
            doLogin(request, response);
        } else if (servletPath.equals(SERVICE_REGISTER)) {
            doRegister(request, response);
        }

    }

    private void doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String userid = request.getParameter("userid");
        String password = request.getParameter("password");
        String field = request.getParameter("field");
        User user = new User(userid, password, null, field);


        UserInitService userInitService = new UserInitServiceImpl();
        String mes = userInitService.doLogin(user);

        if (MES_LOGIN_SUCCESS.equals(mes)) {
            HttpSession session = request.getSession();
            session.setAttribute("UserMessage", user);

            String contextPath = request.getContextPath();
            String flag = request.getParameter("flag");

            if ("1".equals(flag)) {
                Cookie cookieId = new Cookie("userid", userid);
                Cookie cookiePwd = new Cookie("password", password);
                Cookie cookieField = new Cookie("field", field);

                cookieId.setMaxAge(10 * 24 * 60 * 60);
                cookiePwd.setMaxAge(10 * 24 * 60 * 60);
                cookieField.setMaxAge(10 * 24 * 60 * 60);

                cookieId.setPath(contextPath);
                cookiePwd.setPath(contextPath);
                cookieField.setPath(contextPath);

                response.addCookie(cookieId);
                response.addCookie(cookiePwd);
                response.addCookie(cookieField);
            }


            if ("user".equals(field)) {
                response.sendRedirect(contextPath + "/user");
            } else {
                response.sendRedirect(contextPath + "/admin");
            }
        } else {
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter pw = response.getWriter();
            pw.print("<h1 align=\"center\">" + mes + "</h1>");
            pw.print("<a align=\"center\" href=\"login.jsp\">点这里重新登录</a>");
        }

    }

    private void doRegister(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");

        String username = request.getParameter("username");
        String userid = request.getParameter("userid");
        String password = request.getParameter("password");
        String field = request.getParameter("field");

        User user = new User(userid, password, username, field);
        UserInitService userInitService = new UserInitServiceImpl();
        String mes = userInitService.doRegister(user);

        response.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = response.getWriter();
        pw.print("<h1 align=\"center\">" + mes + "</h1>");
    }


}
